// 节流

function 叫(话) {
  console.log(话);
}

function debounce(fn, delay) {
  let timer = null;
  return function () {
    clearTimeout(timer);
    timer = setTimeout(() => fn.apply(this, arguments), delay);
  };
}

function myThrottle(fn, delay = 90) {
  let allow = true;
  let timer = null;
  const debouncedFn = debounce(fn, delay);
  return (...args) => {
    if (!allow) {
      debouncedFn(...args); // 为了解决最后那一下，用debounce
    } else {
      fn(...args);
      allow = false;
      timer = setTimeout(() => {
        allow = true;
      }, delay);
    }
  };
}

const 节流叫 = myThrottle(叫);

节流叫(0);

setTimeout(() => {
  节流叫(20);
}, [20]);

setTimeout(() => {
  节流叫(40);
}, [40]);

setTimeout(() => {
  节流叫(60);
}, [60]);

setTimeout(() => {
  节流叫(80);
}, [80]);

setTimeout(() => {
  节流叫(100);
}, [100]);

setTimeout(() => {
  节流叫(120);
}, [120]);

setTimeout(() => {
  节流叫(140);
}, [140]);

setTimeout(() => {
  节流叫(160);
}, [160]);

// 0
// 100
// 160
